﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Tools.Helper;
using UIWindows.Views;
using XingLucifer.IBase.Enums;

namespace UIWindows.Tools.MesHelper
{
    public class MesGetRunParameterBodyHelper
    {
        /// <summary>
        /// 组开机参数报文
        /// </summary>
        /// <param name="mesParameter"></param>
        /// <param name="parameter"></param>
        /// <param name="deviceConfig"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetRunParameterBody(MesParameterStatic mesParameter, ParameterStatic parameter, DeviceConfigStatic deviceConfig)
        {
            var paramList = GetCoreRunParameter(mesParameter, parameter, deviceConfig);
            Dictionary<string, object> parameters = new Dictionary<string, object>()
            {
                { "machineNo", parameter.CollectionItemsParameter.EquipNum },//设备编码
                { "producttype", parameter.CollectionItemsParameter.ProductType.ToString() },//生产类型
            
                {
                    "parameter", new Dictionary<string, object>()
                    {
                        { "tenantID", parameter.CollectionItemsParameter.ProductionLineNo },//产线编号   
                        { "uploadTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },//上传时间   
                        { "upperCompTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },//上位机当前时间   
                        { "plc1Time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },//PLC1当前时间   
                        { "equipCode", parameter.CollectionItemsParameter.EquipNum },//设备编码
                        { "paramList", paramList},//设备编码
                    }
                },
            };

            return parameters;
        }

        /// <summary>
        /// 组修改参数报文
        /// </summary>
        /// <param name="mesParameter"></param>
        /// <param name="parameter"></param>
        /// <param name="deviceConfig"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetChangParameterBody(MesParameterStatic mesParameter, ParameterStatic parameter, DeviceConfigStatic deviceConfig, TemporaryStatic Temporary)
        {
            
            var paramList = GetCoreRunParameter(mesParameter, parameter, deviceConfig);
            Dictionary<string, object> parameters = new Dictionary<string, object>()
            {
                { "machineNo", parameter.CollectionItemsParameter.EquipNum },//设备编码
                { "producttype", parameter.CollectionItemsParameter.ProductType.ToString() },//生产类型
            
                {
                    "parameter", new Dictionary<string, object>()
                    {
                        { "tenantID", parameter.CollectionItemsParameter.ProductionLineNo },//产线编号   
                        { "userAccout", Temporary.Mes_Account },//用户账号   
                        { "uploadTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },//上传时间   
                        { "equipCode", parameter.CollectionItemsParameter.EquipNum },//设备编码  
                        { "paramList", paramList},//设备编码
                    }
                },
            };
            return parameters;
        }
        /// <summary>
        /// 组开机参数核心报文
        /// </summary>
        /// <param name="mesParameter"></param>
        /// <param name="parameter"></param>
        /// <param name="deviceConfig"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private static List<Dictionary<string, object>> GetCoreRunParameter(MesParameterStatic mesParameter, ParameterStatic parameter, DeviceConfigStatic deviceConfig)
        {
            var paramList = new List<Dictionary<string, object>>();  //参数列表
            try
            {
                foreach (var item in mesParameter.MesRunParameters)
                {
                    if (!item.IsEnable) break;

                    bool isHave = false;
                    string propertyName = string.Empty;
                    object value = null;
                    foreach (PropertyInfo rangeParameter in parameter.RangeParameter.GetType().GetProperties())
                    {
                        var attribute = rangeParameter.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
                        if (attribute != null && attribute.Description == item.SendType)
                        {
                            value = rangeParameter.GetValue(parameter.RangeParameter);
                            isHave = true;
                            propertyName = rangeParameter.Name;
                            break;
                        }
                    }
                    if (!isHave)
                    {
                        var RJ6902CAGX = deviceConfig.RJ6902CAGX.FirstOrDefault(x => x.IsBeingUsed);//现在使用
                        if (RJ6902CAGX != null)
                        {
                            foreach (PropertyInfo rjParameter in RJ6902CAGX.GetType().GetProperties())
                            {
                                var attribute = rjParameter.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
                                if (attribute != null && attribute.Description == item.SendType)
                                {
                                    value = rjParameter.GetValue(parameter.RangeParameter);
                                    propertyName = rjParameter.Name;
                                    isHave = true;
                                    break;
                                }
                            }
                        }
                    }

                    var param = new Dictionary<string, object>
                    {
                       { "paramCode",item.ParamCode},//参数编码
                       { "paramName",item.ParamName},//参数名称
                       { "setValue",""},//值
                    };

                    if (value != null)
                    {
                        var valueType = GetTypeByStringHelper.GetTypeByString(item.DataType);
                        param["setValue"] = valueType == value.GetType() ? value : GetTypeByStringHelper.TryTypeConvert(valueType, value, propertyName);
                    }
                    paramList.Add(param);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("组开机参数核心报文异常", ex);
            }
            return paramList;
        }
    }
}
